package com.apartment.own.service.impl;

import com.apartment.common.core.domain.entity.SysDictData;
import com.apartment.common.utils.uuid.EnumUtils;
import com.apartment.own.response.common.DynamicDic;
import com.apartment.own.response.common.DynamicDicValue;
import com.apartment.own.service.ToolsService;
import com.apartment.system.mapper.SysDictDataMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ToolsServiceImpl implements ToolsService {

    public static final String ENUM_STRING = "ENUM";
    public static final String DIC_STRING = "DIC";
    public static final String ENUM_PACKAGE = "com.apartment.own.domain.enums.";

    @Autowired
    SysDictDataMapper dictDataMapper;

    @Override
    public List<DynamicDic> dynamicDicList(List<String> list) {
        ArrayList<DynamicDic> dynamicDicList = new ArrayList<>();
        //判断类型
        list.forEach(ele -> {
            String front = ele.substring(0, ele.indexOf("_"));
            String rear = ele.substring(front.length()+1);
            DynamicDic dynamicDic = new DynamicDic();
            switch (front){
                case ToolsServiceImpl.ENUM_STRING:
                    dynamicDic = getEnum(ele,rear);
                    break;
                case ToolsServiceImpl.DIC_STRING:
                    dynamicDic = getDic(ele,rear);
                    break;
                default:
            }
            dynamicDicList.add(dynamicDic);

        });

        return dynamicDicList;
    }
    /**
     * des.
     * @author       : RazaJin
     * @param        : str2 完整名字  str 查询的名字
     * @return       :
     * @date         : 2020/8/31 0031 上午 11:15
     */
    private DynamicDic getEnum(String fullName,String str){
        List<HashMap<String, Object>> hashMaps = EnumUtils.iterEnum(ENUM_PACKAGE + str,EnumUtils.INDEX,EnumUtils.VALUE,EnumUtils.SECLET);
        DynamicDic dynamicDic = new DynamicDic();
        dynamicDic.setDicName(fullName);
        ArrayList<DynamicDicValue> dynamicDicValueArrayList = new ArrayList<>();
        hashMaps.forEach(hashmap -> {
            DynamicDicValue dynamicDicValue = new DynamicDicValue();
            dynamicDicValue.setId(Long.parseLong( hashmap.get(EnumUtils.INDEX).toString()));
            dynamicDicValue.setValue(String.valueOf(hashmap.get(EnumUtils.VALUE)));
            dynamicDicValue.setSelect(String.valueOf(hashmap.get(EnumUtils.SECLET)));
            dynamicDicValueArrayList.add(dynamicDicValue);
        });
        dynamicDic.setDynamicDicValues(dynamicDicValueArrayList);
        return dynamicDic;
    }
    private DynamicDic getDic(String fullName,String str){
        DynamicDic dynamicDic = new DynamicDic();
        dynamicDic.setDicName(fullName);
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        ArrayList<DynamicDicValue> dynamicDicValueArrayList = new ArrayList<>();
        wrapper.eq("dict_type",str);
        dictDataMapper.selectList(wrapper).forEach(ele->{
            DynamicDicValue dynamicDicValue = new DynamicDicValue();
            dynamicDicValue.setId(Long.valueOf(ele.getDictValue()));
            dynamicDicValue.setValue(ele.getDictLabel());
            dynamicDicValueArrayList.add(dynamicDicValue);
        });
        dynamicDic.setDynamicDicValues(dynamicDicValueArrayList);
        return dynamicDic;
    }
}
